package com.weasing.res.web.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.weasing.res.domain.WxAdmin;
import com.weasing.res.domain.WxPrivilege;
import com.weasing.res.domain.WxRole;
import com.weasing.res.service.IWxAdminService;
import com.weasing.res.service.IWxPrivilegeService;
import com.weasing.res.service.IWxRoleService;
import com.weasing.res.utils.RESUtils;
import com.weasing.res.utils.StringUtil;
import com.weasing.res.web.action.base.BaseAction;

/**
 * 角色的action
 * 
 * @author 黄龙
 *
 */
@Controller
@Scope("prototype")
public class WxRoleAction extends BaseAction<WxRole> {
	@Autowired
	private IWxRoleService wxRoleService;

	@Autowired
	private IWxAdminService wxAdminService;
	@Autowired
	private IWxPrivilegeService wxPrivilegeService;


	/**
	 * 查询所有角色的属性
	 * 
	 * @return
	 */
	// @RequiresPermissions("role-list")
	public String findAll() {
		List<WxAdmin> findAllAndRole = wxAdminService.findAllAndRole();
		List<WxRole> findAllrole = wxRoleService.findAll();
		RESUtils.getRequest().setAttribute("wxAdminList", findAllAndRole);
		RESUtils.getRequest().setAttribute("wxWxRoleList", findAllrole);
		// ValueStack valueStack = ActionContext.getContext().getValueStack();
		// 存入值
		// valueStack.push(findAllAndRole);
		return "wxRolelist";
	}

	/**
	 * 删除角色信息
	 */
	public void deleteRoleAndPrivilege() {
		// 0 失败 1成功
		String parameter = RESUtils.getRequest().getParameter("delRoleid");
		int roleid = Integer.parseInt(parameter);
		WxRole findById = wxRoleService.findById(roleid);
		try {
		findById.setWxPrivileges(null);
		wxRoleService.delete(findById);

			RESUtils.print("1");
		} catch (Exception e) {
			RESUtils.print("0");
		}
	}

	/**
	 * 编辑角色信息
	 */
	public void editRole() {
		// TODO 先过滤信息
		wxRoleService.update(model);
	}

	/**
	 * 增加角色信息
	 */

	public void addRole() {
		// TODO 先过滤信息
		wxRoleService.save(model);
	}

	/**
	 * 增加 角色 修改角色的权限
	 * 
	 * @author 黄龙
	 */
	public void addRolePrivilege() {
		// 1. 是成功 0.是失败
		try {
			String rolename = RESUtils.getRequest().getParameter("rolename");
			String remark = RESUtils.getRequest().getParameter("remark");
			String[] arrPrivilegeIds = RESUtils.getRequest()
					.getParameterValues("arrPrivilegeIds");
			// ------------------------------------
			if (StringUtil.isNotNulls(rolename) && arrPrivilegeIds.length > 0) {
				// 检查是不是已经有的角色
				WxRole checkUserName = wxRoleService.checkUserName(rolename);
				// 字符串数组转化为 int的set集合
				Set<WxPrivilege> staffsSet = new HashSet<WxPrivilege>();
				for (int i = 0; i < arrPrivilegeIds.length; i++) {

					WxPrivilege findById = wxPrivilegeService
							.findById(Integer.parseInt(arrPrivilegeIds[i]));

					staffsSet.add(findById);
				}
				if (null != checkUserName) {
					// 是一个已经有的角色 先删除所有和角色关联的权限数据
					// checkUserName.getWxPrivileges().clear();
					checkUserName.setWxPrivileges(staffsSet);
					checkUserName.setRemark(remark);
					wxRoleService.update(checkUserName);
				} else {
					// 是一个新的角色
					WxRole wxRole = new WxRole();
					wxRole.setRolename(rolename);
					wxRole.setRemark(remark);
					wxRole.setWxPrivileges(staffsSet);
					wxRoleService.save(wxRole);
				}
			}
			RESUtils.print("1");
		} catch (Exception e) {
			RESUtils.print("0");
			e.printStackTrace();
		}
	}
}
